﻿using ImportDataFileter.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ImportDataFileter.Controllers
{
    public class BaseController : Controller
    {

        //public ImportDataFileterEntitiy DbContext = new ImportDataFileterEntitiy();

        public JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd" };
        //protected override void Dispose(bool disposing)
        //{
        //    DeleteDbContext();
        //    base.Dispose(disposing);
        //}

        //private void DeleteDbContext()
        //{
        //    if (DbContext != null)
        //    {
        //        DbContext.Dispose();
        //        DbContext = null;
        //    }
        //}

        public new ActionResult Json(object data)
        {

            string jsonString =
                Newtonsoft.Json.JsonConvert.SerializeObject(
                data, Newtonsoft.Json.Formatting.None,
                this.JsonSerializerSettings);

            return Content(jsonString, "application/json");
        }
        protected ActionResult JsonContent(object data, int statusCode = 1, string statusMessage = "")
        {
            if (data == null)
            {
                data = new object[0];
            }
            else if (!(data is System.Collections.IEnumerable))
                data = new object[] { data };
            return
            Json(new
            {
                statusCode = statusCode,
                statusMessage,
                data
            });
        }


        protected ActionResult JsonFormat(object data, int statusCode = 1)
        {
            string jsonString =
                Newtonsoft.Json.JsonConvert.SerializeObject(
                new
                {
                    statusCode = statusCode,
                    data
                }, Newtonsoft.Json.Formatting.None,
                this.JsonSerializerSettings
                );
            return Json(new
            {
                statusCode = statusCode,
                data
            }, JsonRequestBehavior.AllowGet);
        }

        protected ActionResult ExecuteJavaScript(string js)
        {
            return Content("<script>" + js + "</script>");
        }
        //弹出错误消息
        protected ActionResult MessageBox(string message, string location = null, string jscode = null, bool closeProgress = true)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("alert(decodeURI('{0}'));", Uri.EscapeDataString(message));

            if (!string.IsNullOrEmpty(jscode))
                sb.Append(jscode);

            if (closeProgress)
            {
                sb.Append("if(window.closeProgress){ window.closeProgress(); }else if(window.parent.closeProgress){ window.parent.closeProgress(); };");
            }

            if (!string.IsNullOrEmpty(location))
                sb.AppendFormat("location=unescape('{0}');", Uri.EscapeDataString(location));

            return ExecuteJavaScript(sb.ToString());
        }

        private string _AdministratorId = null;
        protected string AdministratorId
        {
            get
            {
                if (_AdministratorId == null && Request.Cookies["_"] != null)
                    _AdministratorId = Request.Cookies["_"].Value;
                return _AdministratorId;
            }
            set
            {
                _AdministratorId = value;
                Response.AppendCookie(new HttpCookie("_", value));
            }
        }

        protected ActionResult JsonTest(object data, int statusCode = 1, string statusMessage = "")
        {

            if (data == null)
            {
                data = new object[0];
            }
            else if (!(data is System.Collections.IEnumerable))
                data = new object[] { data };
            return
            Json(new
            {
                statusCode = statusCode,
                statusMessage,
                data
            });
        }

        #region 改变图片颜色

        /// <summary> 
        /// 改变图片的颜色 
        /// </summary> 
        /// <param name="filePath">图片的完整路径</param> 
        /// <param name="colorIndex">改变的颜色，true为灰色，false为彩色</param> 
        protected void update_pixelColor(string filePath, bool colorIndex)
        {
            Bitmap bmp = new Bitmap(Bitmap.FromFile(filePath));

            int value = 0;

            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    if (colorIndex)
                        value = this.GetGrayNumColor(bmp.GetPixel(j, i));
                    else
                        value = this.GetHongNumColor(bmp.GetPixel(j, i));

                    bmp.SetPixel(j, i, Color.FromArgb(value, value, value));
                }
            }
            bmp.Save(filePath + ".gray.jpg");
        }

        /// <summary> 
        /// 获取彩色单点像素 
        /// </summary> 
        /// <param name="posClr">单点像素</param> 
        /// <returns>int</returns> 
        private int GetHongNumColor(Color posClr)
        {
            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
        }

        /// <summary> 
        /// 获取灰色单点像素 
        /// </summary> 
        /// <param name="posClr">单点像素</param> 
        /// <returns>Color</returns> 
        private int GetGrayNumColor(Color posClr)
        {
            //要改变ARGB 
            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
        }

        #endregion 改变图片颜色

    }

}